import java.io.*;
public class rom {
  public static void main (String[] args) throws IOException
  {
	int model;
	InputStreamReader isr = new InputStreamReader(System.in);
	BufferedReader stdin = new BufferedReader(isr);
	System.out.println("Enter the filename of the ROM dump:");
	String romfile = stdin.readLine();
	System.out.print("Enter the model of this ROM (82, 83, 85, 86): ");
	try {
		model = Integer.parseInt(stdin.readLine());
	} catch (NumberFormatException exception) {
		System.out.println("Invalid model.");
		return;
	}
	if (model != 82 && model != 83 && model != 85 && model != 86)
	{
		System.out.println("Invalid model.");
		return;
	}
	//Now open the file
	File emulator = new File("Emulator.bin");
	File rom = new File(romfile);
	InputStream emu = new FileInputStream(emulator);
	InputStream romf = new FileInputStream(rom);
	long fl;
	fl = emulator.length();
	byte[] dataEmulator = new byte[(int) fl];
	emu.read(dataEmulator);
	fl = rom.length();
	byte[] dataROM = new byte[(int) fl];
	romf.read(dataROM);
	emu.close();
	romf.close();
	//Now find the bytes for the stored ROM images and
	//add the appropriate one
	int i = 0x83;
	while (dataEmulator[i] != -1)
	{
		i++;
	}
	if (i > 0x86)
	{
		System.out.println("The Emulator.bin file already contains 4 ROM images. No more may be added.");
		return;
	}
	byte n = 0;
	byte newpages = 0;
	switch (model)
	{
	case 82:
		newpages = 8;
		n = 0;
		break;
	case 83:
		newpages = 13;
		n = 1;
		break;
	case 85:
		newpages = 8;
		n = 2;
		break;
	case 86:
		newpages = 14;
		n = 3;
		break;
	}
	//Check ROM version
	String outmsg = "";
	int checksum = 0, j = 0;
	int patches[];
	if (model == 82)
	{
		for (j = 0; j < (128*1024); j++)
			checksum += dataROM[j];
		if (checksum == 496640)
		{
		//82 ROM 16.0
		outmsg = "TI-82 v16.0";
		patches = new int[] {0xBB, 0xD0, 0xE7, 0xFA, 0x116, 0x592, 0x5CB, 0x209F, 0x4C7D, 0x4CA8, 0x4CED, 0x4CF8, 0x4D01, 0x4D25, 0x4D37, 0x4D41, 0x4D52, 0x4D5B, 0x52C9, 0x52E3, 0x5349, 0x5372, 0x53AA, 0x53B0, 0x5441, 0x545A, 0x5463, 0x558D, 0x5597, 0x55B9, 0x57F4, 0x57FD, 0x581D, 0x582D, 0x5852, 0x585D, 0x5863, 0x7824, 0x7833, 0x783A, 0x7857, 0xA8BA, 0xA8CB, 0xA8D2, 0xA8F3, 0xB1D1, 0xB293, 0xCEDF, 0x100D9, 0x100E4, 0x10115, 0x10120, 0x10138, 0x11B33, 0x11B42, 0x11B49, 0x11B52, 0x11B61, 0x12040, 0x12046, 0x1204D, 0x16563, 0x16601, 0x16607, 0x1660F, 0x16615, 0x16635, 0x1663B, 0x16641, 0x192D6, 0x193F2, 0x193F9, 0x19400, 0x19407, 0x1940E, 0x19415, 0x19421};
		for (j = 0; j < patches.length; j++)
		{
			dataROM[patches[j]] = -128 + 0x6D;
			dataROM[patches[j]+1] = -1;
		}
		dataROM[0x11B30] = -128 + 0x6D;
		dataROM[0x11B31] = -2;
		//On key delay
		dataROM[0x554] = 0x00;
		dataROM[0x555] = 0x03;
		}
		if (checksum == 496896)
		{
		//82 ROM 17.0
		outmsg = "TI-82 v17.0";
		patches = new int[] {0xBB, 0xD0, 0xE7, 0xFA, 0x116, 0x592, 0x5CB, 0x209F, 0x4C7D, 0x4CA8, 0x4CED, 0x4CF8, 0x4D01, 0x4D25, 0x4D37, 0x4D41, 0x4D52, 0x4D5B, 0x52C9, 0x52E3, 0x5349, 0x5372, 0x53AA, 0x53B0, 0x5441, 0x545A, 0x5463, 0x558D, 0x5597, 0x55B9, 0x57F4, 0x57FD, 0x581D, 0x582D, 0x5852, 0x585D, 0x5863, 0x7824, 0x7833, 0x783A, 0x7857, 0xA8BA, 0xA8CB, 0xA8D2, 0xA8F3, 0xB1D1, 0xB293, 0xCEDF, 0x100D9, 0x100E4, 0x10115, 0x10120, 0x10138, 0x11B33, 0x11B42, 0x11B49, 0x11B52, 0x11B61, 0x12040, 0x12046, 0x1204D, 0x16569, 0x16607, 0x1660D, 0x16615, 0x1661B, 0x1663B, 0x16641, 0x16647, 0x192D6, 0x193F2, 0x193F9, 0x19400, 0x19407, 0x1940E, 0x19415, 0x19421};
		for (j = 0; j < patches.length; j++)
		{
			dataROM[patches[j]] = -128 + 0x6D;
			dataROM[patches[j]+1] = -1;
		}
		//Replace LCD copy with routine in ED,FE opcode
		dataROM[0x11B30] = -128 + 0x6D;
		dataROM[0x11B31] = -2;
		//On key delay
		dataROM[0x554] = 0x00;
		dataROM[0x555] = 0x03;
		}
		if (checksum == 501248)
		{
		//82 ROM 18.0
		outmsg = "TI-82 v18.0";
		patches = new int[] {0xBB, 0xD0, 0xE7, 0xFA, 0x116, 0x592, 0x5CB, 0x209F, 0x4C7D, 0x4CA8, 0x4CED, 0x4CF8, 0x4D01, 0x4D25, 0x4D37, 0x4D41, 0x4D52, 0x4D5B, 0x52C9, 0x52E3, 0x5349, 0x5372, 0x53AA, 0x53B0, 0x5441, 0x545A, 0x5463, 0x558D, 0x5597, 0x55B9, 0x57F4, 0x57FD, 0x581D, 0x582D, 0x5852, 0x585D, 0x5863, 0x7824, 0x7833, 0x783A, 0x7857, 0xA8BA, 0xA8CB, 0xA8D2, 0xA8F3, 0xB1DB, 0xB29D, 0xCEDF, 0x100D9, 0x100E4, 0x10115, 0x10120, 0x10138, 0x11B33, 0x11B42, 0x11B49, 0x11B52, 0x11B61, 0x12040, 0x12046, 0x1204D, 0x16569, 0x16607, 0x1660D, 0x16615, 0x1661B, 0x1663B, 0x16641, 0x16647, 0x192E5, 0x19401, 0x19408, 0x1940F, 0x19416, 0x1941D, 0x19424, 0x19430};
		for (j = 0; j < patches.length; j++)
		{
			dataROM[patches[j]] = -128 + 0x6D;
			dataROM[patches[j]+1] = -1;
		}
		//Replace LCD copy with routine in ED,FE opcode
		dataROM[0x11B30] = -128 + 0x6D;
		dataROM[0x11B31] = -2;
		//On key delay
		dataROM[0x554] = 0x00;
		dataROM[0x555] = 0x03;
		}
		if (checksum == 493312)
		{
		//82 ROM 19.0
		outmsg = "TI-82 v19.0";
		patches = new int[] {0xBB, 0xD0, 0xE7, 0xFA, 0x116, 0x592, 0x5CB, 0x20B9, 0x4C7D, 0x4CA8, 0x4CED, 0x4CF8, 0x4D01, 0x4D25, 0x4D37, 0x4D41, 0x4D52, 0x4D5B, 0x52C9, 0x52E3, 0x5349, 0x5372, 0x53AA, 0x53B0, 0x5441, 0x545A, 0x5463, 0x558D, 0x5597, 0x55B9, 0x57F4, 0x57FD, 0x581D, 0x582D, 0x5852, 0x585D, 0x5863, 0x7824, 0x7833, 0x783A, 0x7857, 0xA8C3, 0xA8D4, 0xA8DB, 0xA8FC, 0xB1EB, 0xB2AD, 0xCEDF, 0x100D9, 0x100E4, 0x10115, 0x10120, 0x10138, 0x11B33, 0x11B42, 0x11B49, 0x11B52, 0x11B61, 0x12040, 0x12046, 0x1204D, 0x16569, 0x16607, 0x1660D, 0x16615, 0x1661B, 0x1663B, 0x16641, 0x16647, 0x192E5, 0x19401, 0x19408, 0x1940F, 0x19416, 0x1941D, 0x19424, 0x19430};
		for (j = 0; j < patches.length; j++)
		{
			dataROM[patches[j]] = -128 + 0x6D;
			dataROM[patches[j]+1] = -1;
		}
		//LCD copy
		dataROM[0x11B30] = -128 + 0x6D;
		dataROM[0x11B31] = -2;
		//On key delay
		dataROM[0x554] = 0x00;
		dataROM[0x555] = 0x03;		
		}
		if (checksum == 456607)
		{
		//82 ROM 19.006
		outmsg = "TI-82 v19.006";
		patches = new int[] { 0xBB, 0xD0, 0xE7, 0xFA, 0x135, 0x5E5, 0x611, 0x21E4, 0x4C7D, 0x4CA8, 0x4CED, 0x4CF8, 0x4D01, 0x4D25, 0x4D37, 0x4D41, 0x4D52, 0x4D5B, 0x52C9, 0x52E3, 0x5349, 0x5372, 0x53AA, 0x53B0, 0x5441, 0x545A, 0x5463, 0x558D, 0x5597, 0x55B9, 0x57F4, 0x57FD, 0x581D, 0x582D, 0x5852, 0x585D, 0x5863, 0x7824, 0x7833, 0x783A, 0x7857, 0xA8BA, 0xA8CB, 0xA8D2, 0xA8F3, 0xB1DA, 0xB2CD, 0xCEDF, 0x100D9, 0x100E4, 0x10115, 0x10120, 0x10138, 0x11B33, 0x11B42, 0x11B49, 0x11B52, 0x11B61, 0x12040, 0x12046, 0x1204D, 0x16561, 0x165FF, 0x16605, 0x1660D, 0x16613, 0x16633, 0x16639, 0x1663F, 0x192EC, 0x19408, 0x1940F, 0x19416, 0x1941D, 0x19424, 0x1942B, 0x19437};
		for (j = 0; j < patches.length; j++)
		{
			dataROM[patches[j]] = -128 + 0x6D;
			dataROM[patches[j]+1] = -1;
		}
		//LCD copy
		dataROM[0x11B30] = -128 + 0x6D;
		dataROM[0x11B31] = -2;
		//On key delay
		dataROM[0x5A7] = 0x00;
		dataROM[0x5A8] = 0x03;
		//Link port patching
		patches = new int[] { 0x2F6B, 0xA984, 0xA986, 0xA9AF, 0xA9B4, 0xA9BA, 0xA9C1, 0xA9DD, 0xA9DF, 0xAAB5, 0xAAB7, 0xAACC, 0xAAD0, 0xAAD7, 0xAADB, 0xAAE9, 0xAAED, 0xAAF8, 0xAAFA, 0xAB12, 0xAB14, 0xAB1C, 0xAB27, 0xAB29, 0xB1EB, 0xB9AF, 0x19258, 0x1925A};
		byte[] patchvalues = new byte[] { -0x40, 0x03, 0x03, -0x18, -0x2C, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, -0x2C, 0x03, 0x02, 0x03, 0x03, -0x18, 0x03, 0x01, 0x03, -0x04, 0x03, 0x03};
		for (j = 0; j < patches.length; j++)
		{
			dataROM[patches[j]] = patchvalues[j];
		}
		}
	}
	if (model == 83)
	{
		for (j = 0; j < (128*1024); j++)
			checksum += dataROM[j];
		switch (checksum)
		{
			case 632323:
				outmsg = "TI-83 v1.02";
				//LCD copy
				patches = new int[] { 0x135, 0x14A, 0x161, 0x174, 0x190, 0x62A, 0x656, 0x20CF, 0x52CF, 0x52FA, 0x533E, 0x5349, 0x5352, 0x5376, 0x5388, 0x5392, 0x53A3, 0x53AC, 0x5915, 0x592F, 0x59B8, 0x59E1, 0x5A19, 0x5A1F, 0x5B1C, 0x5B35, 0x5B3E, 0x5C68, 0x5C72, 0x5C94, 0x5EB3, 0x5EBE, 0x5EC4, 0x7304, 0x7313, 0x731A, 0x7337, 0xC046, 0xC0E5, 0xC0EB, 0xC0F3, 0xC0F9, 0xC119, 0xC11F, 0xC125, 0x101EB, 0x101F6, 0x10225, 0x12105, 0x12114, 0x1211B, 0x1212B, 0x1213A, 0x1214B, 0x12575, 0x1257B, 0x12582, 0x188C4, 0x189E1, 0x189E8, 0x189EF, 0x189F6, 0x189FD, 0x18A04, 0x18A10, 0x1CBB3, 0x1CBC4, 0x1CBCB, 0x1CBEC, 0x1D6CB, 0x1E923, 0x1E92C, 0x1E94C, 0x1E95C, 0x20135, 0x2014A, 0x20161, 0x20174, 0x20190, 0x2062A, 0x20656, 0x220CF, 0x245F0};
				for (j = 0; j < patches.length; j++)
				{
					dataROM[patches[j]] = -128 + 0x6D;
					dataROM[patches[j]+1] = -1;
				}
				//LCD copy
				dataROM[0x12102] = -128 + 0x6D;
				dataROM[0x12103] = -2;
				//On key delay
				dataROM[0x5EC] = 0x00;
				dataROM[0x5ED] = 0x03;
				break;
			case 631904:
				outmsg = "TI-83 v1.03";
				//LCD copy
				patches = new int[] {0x135, 0x14A, 0x161, 0x174, 0x190, 0x62A, 0x656, 0x20CF, 0x52CF, 0x52FA, 0x533E, 0x5349, 0x5352, 0x5376, 0x5388, 0x5392, 0x53A3, 0x53AC, 0x5915, 0x592F, 0x59B8, 0x59E1, 0x5A19, 0x5A1F, 0x5B1C, 0x5B35, 0x5B3E, 0x5C68, 0x5C72, 0x5C94, 0x5EB3, 0x5EBE, 0x5EC4, 0x730B, 0x731A, 0x7321, 0x733E, 0xC046, 0xC0E5, 0xC0EB, 0xC0F3, 0xC0F9, 0xC119, 0xC11F, 0xC125, 0x101EB, 0x101F6, 0x10225, 0x12105, 0x12114, 0x1211B, 0x1212B, 0x1213A, 0x1214B, 0x12575, 0x1257B, 0x12582, 0x188C4, 0x189E1, 0x189E8, 0x189EF, 0x189F6, 0x189FD, 0x18A04, 0x18A10, 0x1CBB3, 0x1CBC4, 0x1CBCB, 0x1CBEC, 0x1D6CB, 0x1E925, 0x1E92E, 0x1E94E, 0x1E95E, 0x20135, 0x2014A, 0x20161, 0x20174, 0x20190, 0x2062A, 0x20656, 0x220CF, 0x245F0};
				for (j = 0; j < patches.length; j++)
				{
					dataROM[patches[j]] = -128 + 0x6D;
					dataROM[patches[j]+1] = -1;
				}
				//LCD copy
				dataROM[0x12102] = -128 + 0x6D;
				dataROM[0x12103] = -2;
				//On key
				dataROM[0x5EC] = 0x00;
				dataROM[0x5ED] = 0x03;
				break;
			case 631987:
				outmsg = "TI-83 v1.04";
				//LCD copy
				patches = new int[] {0x135, 0x14A, 0x161, 0x174, 0x190, 0x62A, 0x656, 0x20CF, 0x52CF, 0x52FA, 0x533E, 0x5349, 0x5352, 0x5376, 0x5388, 0x5392, 0x53A3, 0x53AC, 0x5915, 0x592F, 0x59B8, 0x59E1, 0x5A19, 0x5A1F, 0x5B1C, 0x5B35, 0x5B3E, 0x5C68, 0x5C72, 0x5C94, 0x5EB3, 0x5EBE, 0x5EC4, 0x730B, 0x731A, 0x7321, 0x733E, 0xC046, 0xC0E5, 0xC0EB, 0xC0F3, 0xC0F9, 0xC119, 0xC11F, 0xC125, 0x101EB, 0x101F6, 0x10225, 0x12105, 0x12114, 0x1211B, 0x1212B, 0x1213A, 0x1214B, 0x12575, 0x1257B, 0x12582, 0x188C4, 0x189E1, 0x189E8, 0x189EF, 0x189F6, 0x189FD, 0x18A04, 0x18A10, 0x1CBB3, 0x1CBC4, 0x1CBCB, 0x1CBEC, 0x1D6CB, 0x1E925, 0x1E92E, 0x1E94E, 0x1E95E, 0x20135, 0x2014A, 0x20161, 0x20174, 0x20190, 0x2062A, 0x20656, 0x220CF, 0x245F0};
				for (j = 0; j < patches.length; j++)
				{
					dataROM[patches[j]] = -128 + 0x6D;
					dataROM[patches[j]+1] = -1;
				}
				//LCD copy
				dataROM[0x12102] = -128 + 0x6D;
				dataROM[0x12103] = -2;
				//On key
				dataROM[0x5EC] = 0x00;
				dataROM[0x5ED] = 0x03;
				break;
			case 596116:
				outmsg = "TI-83 v1.06";
				//LCD copy
				patches = new int[] {0x135, 0x14A, 0x161, 0x174, 0x190, 0x62A, 0x656, 0x20E0, 0x52CF, 0x52FA, 0x533E, 0x5349, 0x5352, 0x5376, 0x5388, 0x5392, 0x53A3, 0x53AC, 0x5915, 0x592F, 0x59B8, 0x59E1, 0x5A19, 0x5A1F, 0x5B1C, 0x5B35, 0x5B3E, 0x5C68, 0x5C72, 0x5C94, 0x5EB3, 0x5EBE, 0x5EC4, 0x730B, 0x731A, 0x7321, 0x733E, 0xC046, 0xC0E5, 0xC0EB, 0xC0F3, 0xC0F9, 0xC119, 0xC11F, 0xC125, 0x101EB, 0x101F6, 0x10225, 0x12105, 0x12114, 0x1211B, 0x1212B, 0x1213A, 0x1214B, 0x12575, 0x1257B, 0x12582, 0x188C4, 0x189E1, 0x189E8, 0x189EF, 0x189F6, 0x189FD, 0x18A04, 0x18A10, 0x1CBB3, 0x1CBC4, 0x1CBCB, 0x1CBEC, 0x1D6CB, 0x1E925, 0x1E92E, 0x1E94E, 0x1E95E, 0x20135, 0x2014A, 0x20161, 0x20174, 0x20190, 0x2062A, 0x20656, 0x220E0, 0x245F0};
				for (j = 0; j < patches.length; j++)
				{
					dataROM[patches[j]] = -128 + 0x6D;
					dataROM[patches[j]+1] = -1;
				}
				//LCD copy
				dataROM[0x12102] = -128 + 0x6D;
				dataROM[0x12103] = -2;
				//On key
				dataROM[0x5EC] = 0x00;
				dataROM[0x5ED] = 0x03;
				break;
			case 633709:
				outmsg = "TI-83 v1.07";
				//LCD copy
				patches = new int[] {0x135, 0x14A, 0x161, 0x174, 0x190, 0x62A, 0x656, 0x20CF, 0x52CF, 0x52FA, 0x533E, 0x5349, 0x5352, 0x5376, 0x5388, 0x5392, 0x53A3, 0x53AC, 0x5915, 0x592F, 0x59B8, 0x59E1, 0x5A19, 0x5A1F, 0x5B1C, 0x5B35, 0x5B3E, 0x5C68, 0x5C72, 0x5C94, 0x5EB3, 0x5EBE, 0x5EC4, 0x730B, 0x731A, 0x7321, 0x733E, 0xC046, 0xC0E5, 0xC0EB, 0xC0F3, 0xC0F9, 0xC119, 0xC11F, 0xC125, 0x101EB, 0x101F6, 0x10225, 0x12105, 0x12114, 0x1211B, 0x1212B, 0x1213A, 0x1214B, 0x12575, 0x1257B, 0x12582, 0x188C4, 0x189E1, 0x189E8, 0x189EF, 0x189F6, 0x189FD, 0x18A04, 0x18A10, 0x1CBB3, 0x1CBC4, 0x1CBCB, 0x1CBEC, 0x1D6CB, 0x1E925, 0x1E92E, 0x1E94E, 0x1E95E, 0x20135, 0x2014A, 0x20161, 0x20174, 0x20190, 0x2062A, 0x20656, 0x220CF, 0x245F0};
				for (j = 0; j < patches.length; j++)
				{
					dataROM[patches[j]] = -128 + 0x6D;
					dataROM[patches[j]+1] = -1;
				}
				//LCD copy
				dataROM[0x12102] = -128 + 0x6D;
				dataROM[0x12103] = -2;
				//On key
				dataROM[0x5EC] = 0x00;
				dataROM[0x5ED] = 0x03;
				break;
			case 641476:
				outmsg = "TI-83 v1.08";
				//LCD copy
				patches = new int[] {0x135, 0x14A, 0x161, 0x174, 0x1AA, 0x652, 0x67E, 0x2109, 0x52CF, 0x52FA, 0x533E, 0x5349, 0x5352, 0x5376, 0x5388, 0x5392, 0x53A3, 0x53AC, 0x5915, 0x592F, 0x59B8, 0x59E1, 0x5A19, 0x5A1F, 0x5B1C, 0x5B35, 0x5B3E, 0x5C68, 0x5C72, 0x5C94, 0x5EB3, 0x5EBE, 0x5EC4, 0x730B, 0x731A, 0x7321, 0x733E, 0xC046, 0xC0E5, 0xC0EB, 0xC0F3, 0xC0F9, 0xC119, 0xC11F, 0xC125, 0x101EB, 0x101F6, 0x10225, 0x12105, 0x12114, 0x1211B, 0x1212B, 0x1213A, 0x1214B, 0x12575, 0x1257B, 0x12582, 0x188C4, 0x189E1, 0x189E8, 0x189EF, 0x189F6, 0x189FD, 0x18A04, 0x18A10, 0x1CBB3, 0x1CBC4, 0x1CBCB, 0x1CBEC, 0x1D6CB, 0x1E925, 0x1E92E, 0x1E94E, 0x1E95E, 0x20135, 0x2014A, 0x20161, 0x20174, 0x201AA, 0x20652, 0x2067E, 0x22109, 0x245F0};
				for (j = 0; j < patches.length; j++)
				{
					dataROM[patches[j]] = -128 + 0x6D;
					dataROM[patches[j]+1] = -1;
				}
				//LCD copy
				dataROM[0x12102] = -128 + 0x6D;
				dataROM[0x12103] = -2;
				//On key
				dataROM[0x614] = 0x00;
				dataROM[0x615] = 0x03;
				break;
			case 631156:
				outmsg = "TI-83 v1.10";
				//LCD copy
				patches = new int[] {0x135, 0x14A, 0x161, 0x174, 0x1AF, 0x657, 0x683, 0x210E, 0x52CF, 0x52FA, 0x533E, 0x5349, 0x5352, 0x5376, 0x5388, 0x5392, 0x53A3, 0x53AC, 0x5915, 0x592F, 0x59B8, 0x59E1, 0x5A19, 0x5A1F, 0x5B1C, 0x5B35, 0x5B3E, 0x5C68, 0x5C72, 0x5C94, 0x5EB3, 0x5EBE, 0x5EC4, 0x730B, 0x731A, 0x7321, 0x733E, 0xC046, 0xC0E5, 0xC0EB, 0xC0F3, 0xC0F9, 0xC119, 0xC11F, 0xC125, 0x101EB, 0x101F6, 0x10225, 0x12105, 0x12114, 0x1211B, 0x1212B, 0x1213A, 0x1214B, 0x12575, 0x1257B, 0x12582, 0x188C4, 0x189E1, 0x189E8, 0x189EF, 0x189F6, 0x189FD, 0x18A04, 0x18A10, 0x1CBB3, 0x1CBC4, 0x1CBCB, 0x1CBEC, 0x1D6CB, 0x1E925, 0x1E92E, 0x1E94E, 0x1E95E, 0x20135, 0x2014A, 0x20161, 0x20174, 0x201AF, 0x20657, 0x20683, 0x2210E, 0x245F0};
				for (j = 0; j < patches.length; j++)
				{
					dataROM[patches[j]] = -128 + 0x6D;
					dataROM[patches[j]+1] = -1;
				}
				//LCD copy
				dataROM[0x12102] = -128 + 0x6D;
				dataROM[0x12103] = -2;
				//On key
				dataROM[0x619] = 0x00;
				dataROM[0x61A] = 0x03;
				//Special patch for initialization
				dataROM[0x6FC] = 0x16;
				break;
		}
	}
	if (model == 85)
	{
		for (j = 0; j < (128*1024); j++)
			checksum += dataROM[j];
		switch (checksum)
		{
			case 534272:
				outmsg = "TI-85 v3.0a";
				dataROM[0xAF1] = 0x00;
				dataROM[0xAF2] = 0x03;
				break;
			case 628224:
				outmsg = "TI-85 v9.0";
				dataROM[0xAEE] = 0x00;
				dataROM[0xAEF] = 0x03;
				break;
			case 659712:
				outmsg = "TI-85 v10.0";
				dataROM[0xAEE] = 0x00;
				dataROM[0xAEF] = 0x03;
				break;
		}
	}
	if (model == 86)
	{
		for (j = 0; j < (256*1024); j++)
			checksum += dataROM[j];
		if (checksum == 979968)
		{
		//v1.2
		outmsg = "TI-86 v1.2";
		dataROM[0x1C73D] = -128 + 0x40;
		//Patch the ON key delay
		dataROM[0xBD6] = 0x00;
		dataROM[0xBD7] = 0x03;
		}
		if (checksum == 958464)
		{
		//v1.3
		outmsg = "TI-86 v1.3";
		dataROM[0x1C73D] = -128 + 0x40;
		//Patch the ON key delay
		dataROM[0xBD6] = 0x00;
		dataROM[0xBD7] = 0x03;
		}
		if (checksum == 940800)
		{
		//v1.4
		outmsg = "TI-86 v1.4";
		dataROM[0x1C73D] = -128 + 0x40;
		//Patch ON key
		dataROM[0xBD6] = 0x00;
		dataROM[0xBD7] = 0x03;
		}
		if (checksum == 967705)
		{
		//v1.5
		outmsg = "TI-86 v1.5";
		dataROM[0x1C73D] = -128 + 0x40;
		//Patch ON key
		dataROM[0xBE1] = 0x00;
		dataROM[0xBE2] = 0x03;
		}
		if (checksum == 966400)
		{
		//v1.6
		outmsg = "TI-86 v1.6";
		//Patch it to 64KB of RAM
		dataROM[0x1C73D] = -128 + 0x40;
		//Patch the ON key delay
		dataROM[0xBE1] = 0x00;
		dataROM[0xBE2] = 0x03;
		}
	}
	System.out.println(outmsg);
	dataEmulator[i] = n;
	//Update page count
	dataEmulator[0x1C] += newpages;
	//Patch binary
	FileOutputStream fout = new FileOutputStream(emulator);
	fout.write(dataEmulator);
	int aaa = 0;
	if (model == 86)
		aaa = 2;
	if (model == 83)
		aaa = 3;
	for (i = 0; i < (dataROM.length)-(16384*aaa); i++)
		fout.write(dataROM[i]);
	//fout.write(dataROM);
	fout.close();
	//Create ROM!
	FileInputStream ftemp = new FileInputStream(emulator);
	byte[] appData = new byte[(int) emulator.length()];
	ftemp.read(appData);
	ftemp.close();
	FileWriter myout = new FileWriter("Emulator.hex");
	int page = 0;
	int scheck = 0;
	double aa = (emulator.length() / 16384);
	//if (model == 86)
	//	aa -= 2;
	//if (model == 83)
	//	aa -= 3;
	for (i = 0; i < aa; i++)
	{
		myout.write(":0200000200");
		scheck = 0xFC - page;
		myout.write(hexvalue(page) + hexvalue(scheck) + "\r\n");
		if (i == (aa - 1))
			{ WritePage(appData,16384*i,myout,true); } else { WritePage(appData,16384*i,myout, false); }
		page++;
	}
	myout.write(":00000001FF");
	myout.close();
  }
	public static void WritePage(byte[] input, int offset, FileWriter fh, boolean chop) throws IOException
	{
		int a;
		int b;
		int c = 512;
		if (chop == true)
			c = 509;
		int address = 0x4000;
		for (a = 0; a<c; a++)
		{
			address = 0x4000 + (a*32);
			fh.write(":20" + Integer.toString(address, 16).toUpperCase() + "00");
			for (b = 0; b<32; b++)
			{
				fh.write(hexvalue(input[offset+(a*32)+b]));
			}
			fh.write(checksum(input, offset+(a*32), 32, address));
			fh.write("\r\n");
		}
	}
	public static String hexvalue(int hex)
	{
		if (hex < 0)
			hex += 0x100;
		return Integer.toString(hex + 0x100, 16).substring(1).toUpperCase();
 
	}
	public static String checksum(byte[] input, int start, int length, int address)
	{
		int checksum = 32;
		int i;
		checksum += (address & 0xff) + (address >> 8);
		for (i=0; i<length; i++)
		{
			if (input[start+i] < 0)
			{ checksum += 0x100 + input[start+i]; } else
			{ checksum += input[start+i]; }
		}
		checksum = 0x100 - checksum;
		return hexvalue(checksum & 0xFF);
	}
}